package com.smasher.widget.base

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

abstract class BaseBindingFragment<VB : ViewBinding> : BaseFragment() {


    // This property is only valid between onCreateView and
    // onDestroyView.
    lateinit var mBinding: VB

    override fun getRootViewDelegate(inflater: LayoutInflater, container: ViewGroup?): View {
        mBinding = getViewBindingForFragment(inflater, container)
        return mBinding.root
    }

    @Suppress("UNCHECKED_CAST")
    private fun getViewBindingForFragment(
        layoutInflater: LayoutInflater,
        container: ViewGroup?
    ): VB {
        val type = javaClass.genericSuperclass as ParameterizedType
        val aClass = type.actualTypeArguments[0] as Class<*>
        val method = aClass.getDeclaredMethod(
            "inflate",
            LayoutInflater::class.java,
            ViewGroup::class.java,
            Boolean::class.java
        )
        return method.invoke(null, layoutInflater, container, false) as VB
    }


}